/*
 * @Description: string
 * @Author: klisit
 * @Date: 2021-01-10 17:27:29
 * @LastEditTime: 2021-01-10 17:27:39
 * @LastEditors: klisit
 */
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: () => void): void {
    const { method, baseUrl, body, query, ip } = req;
    console.log(ip);

    switch (method) {
      case 'GET':
        Logger.log(`\x1B[44;30m ${baseUrl} \x1B[0m query:${JSON.stringify(query)}`, method, false);
        break;
      case 'POST':
        Logger.log(`\x1B[44;30m ${baseUrl} \x1B[0m body:${JSON.stringify(body)}`, method, false);
        break;
      case 'PUT':
        Logger.log(`\x1B[43;30m ${baseUrl} \x1B[0m body:${JSON.stringify(body)}`, method, false);
        break;
      case 'PATCH':
        Logger.log(`\x1B[43;30m ${baseUrl} \x1B[0m body:${JSON.stringify(body)}`, method, false);
        break;
      case 'DELETE':
        Logger.log(`\x1B[41;30m ${baseUrl} \x1B[0m`, method, false);
        break;

      default:
        Logger.log(`\x1B[47;30m ${baseUrl} \x1B[0m`, method, false);
        break;
    }

    next();
  }
}
